import sys

def noop(*lst) :
  "Input any number of arguments and nothing."
  1

def lstrips(lst) :
  return [x.strip() for x in lst]

def val(x) :
  """Return a value; if passed a lambda, then run it."""
  def functionp(x) : return type(x).__name__ == 'function' 
  return x() if functionp(x) else x

def inc(d,key,delta=1):
  """Increment a counter at key[d]."""
  n = d[key] = d.get(key,0) + delta
  return n

def egs(*lst):
  """Simple regression tool: runs all the functions in 'lst'."""
  for i in lst: 
    print '\n----|',i.func_name,'|','-'*40,'\n'
    i()

def often(seq,total,item,weight) :
  """
  Iterator. Selects with some bias from a sequence of items x.
  'Item' and 'weight' are lambda bodies.
  Each item(x) contributes weight(x) to a 'total' probability.
  The sum of all contributions is 'total'.
  """
  while True:
    chosen = random.uniform(0, total)
    count  = 0
    for x in seq:
      count += weight(x)
      if count > chosen:
        yield item(x)
        break


def hasArgs():
  "Returns true if the first arg has the form -com."
  return  len(sys.argv) > 1 and  '-' in sys.argv[1]

def runArgs() :
  """
   Utility for calling functions from command line.
   E.g. python file.py -function arg1 ag2 arg3 ...
   will generate the string  function(arg1,arg2,arg3,...).
  """
  def protect(string, fail=None):
    if string == 'True': return True
    if string == 'False':return False
    try:
      tmp = float(string)
      if tmp % 1 == 0:
        tmp = int(tmp)
        return tmp
    except Exception:
      return "'" + string + "'" 
  what = re.sub(r'^-',"",sys.argv[1])
  args=""
  if len(sys.argv) > 2:
    args = map(protect,sys.argv[2:])
    args = ", ".join(map(str,args))
  call = what + '(' + args + ')'
  return call
